﻿using ScreenMonitor.ServerLib;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ServerUI
{
    public partial class ClientList : WeifenLuo.WinFormsUI.Docking.DockContent
    {

        private class ClientItem
        {
            public ClientObject Obj;
            public TreeNode Tnode;
        }

        private List<ClientItem> _ClientList = new List<ClientItem>();

        public ClientList()
        {
            InitializeComponent();
        }

        public void AddClient(ClientObject obj)
        {
            ClientItem Item = new ClientItem();
            Item.Obj = obj;
            TreeNode Tnode = new TreeNode();
            Tnode.Text = obj.InfoStr;
            Item.Tnode = Tnode;
            Tnode.Tag = obj;
            _ClientList.Add(Item);
            obj.StateChanged += obj_StateChanged;
            obj_StateChanged(obj, new EventArgs());
        }

        void obj_StateChanged(object sender, EventArgs e)
        {
            if (this.clientListTView.InvokeRequired)
            {
                EventHandler del = new EventHandler(obj_StateChanged);
                this.clientListTView.BeginInvoke(del, sender, e);
            }
            else
            {
                ClientObject obj = (ClientObject)sender;
                ClientItem item = null;
                foreach (ClientItem i in _ClientList)
                {
                    if (i.Obj == obj)
                    {
                        item = i;
                        break;
                    }
                }
                if (item != null)
                {

                    switch (item.Obj.State)
                    {
                        case ClientState.Connected:
                            if (item.Tnode.Parent != clientListTView.Nodes[0])
                            {
                                if(item.Tnode.Parent!=null)
                                    item.Tnode.Remove();
                                this.clientListTView.Nodes[0].Nodes.Add(item.Tnode);
                            }
                            break;
                        case ClientState.DisConnected:
                            if (item.Tnode.Parent != clientListTView.Nodes[1])
                            {
                                if (item.Tnode.Parent != null)
                                    item.Tnode.Remove();
                                this.clientListTView.Nodes[1].Nodes.Add(item.Tnode);
                            }
                            break;
                    }
                }
            }
        }

        private void ClientList_Load(object sender, EventArgs e)
        {

        }

        private void propertyGrid1_Click(object sender, EventArgs e)
        {

        }

        private void splitContainer1_SplitterMoved(object sender, SplitterEventArgs e)
        {

        }

        private void clientListTView_AfterSelect(object sender, TreeViewEventArgs e)
        {
            if (clientListTView.SelectedNode != null)
            {
                Program.ProperWnd.SetObject(clientListTView.SelectedNode.Tag);
            }
        }
    }
}
